/*
 *  Copyright 2008-2010 www.jrails.org
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.rails.extjs;

import org.rails.extjs.BoxComponent;
import org.rails.extjs.Option;
import org.rails.extjs.ajax.IWait;
import org.rails.extjs.javascript.Function;
import org.rails.extjs.javascript.JavaScript;

public class ProgressBar extends BoxComponent implements IWait {
	
	protected int duration;
	protected int interval = 100;
	protected int increment = 10;
	
	protected Boolean animate = false;
	protected String text;
	protected String doneText;
	
	protected Function fn;

	public ProgressBar() {
		super();
	}

	public ProgressBar(Option configOptions) {
		super(configOptions);
	}

	public ProgressBar(String id) {
		super(id);
	}
	
	public void setDoneText(String doneText){
		this.doneText = doneText;
	}
	
	public String getDoneText(){
		return doneText;
	}
	
	public void setFunction(Function fn){
		this.fn = fn;
	}
	
	public Function getFunction(){
		return fn;
	}
	
	public void setDuration(int duration){
		this.duration = duration;
	}
	
	public int getDuration(){
		return duration;
	}
	
	public void setInterval(int interval){
		this.interval = interval;
	}
	
	public int getInterval(){
		return interval;
	}
	
	public void setIncrement(int increment){
		this.increment = increment;
	}
	
	public int getIncrement(){
		return increment;
	}
	
	public void setAnimate(Boolean animate){
		this.animate = animate;
	}
	
	public Boolean isAnimate(){
		return animate;
	}
	
	public void setBaseCls(String baseCls){
		addOption("baseCls", baseCls);
	}
	
	public String getBaseCls(){
		return getString("baseCls","x-progress");
	}
	
	public void setText(String text){
		this.text = text;
	}
	
	public String getText(){
		return text;
	}
	
	public void setTextEl(String textEl){
		addOption("textEl",textEl);
	}
	
	public String getTextEl(){
		return getString("textEl","");
	}
	
	public void setValue(Integer value){
		addOption("value", value);
	}
	
	public Integer getValue(){
		return getInteger("value",0);
	}
	
	protected Option waitConfig(){
		Option cf = new Option();
		cf.put("duration",duration);
		cf.put("interval",interval);
		cf.put("increment",increment);
		cf.put("animate",animate);
		if(text != null)
			cf.put("text",text);
		if(fn != null)
			cf.put("fn",fn);
		return cf;
	}
 	
//	@Override
	public JavaScript show() {
		JavaScript js = new JavaScript();
		js.append("Ext.getCmp('{0}').wait(",getId());
		js.append(waitConfig().createJson());
		js.append(");");
		js.append("Ext.getCmp('{0}').show();", getId());
		return js;
	}
	
//	@Override
	public JavaScript hide() {
		JavaScript js = new JavaScript();
		js.append("Ext.getCmp('{0}').reset();", getId());
		if(doneText != null || doneText.equals(""))
			js.append("Ext.getCmp('{0}').updateText({1});", getId(),escape(doneText));
		if(doneText == null)
			js.append("Ext.getCmp('{0}').hide();", getId());
		return js;
	}
	
//	@Override
	public JavaScript error() {
		return new JavaScript("alert('Status: ' + res.status + ' ' + res.statusText);").append(hide());
	}

//	@Override
	public String getScript() {
		script = new StringBuffer("");
		createItems();
		script.append("new Ext.ProgressBar(");
		script.append(createOptions());
		script.append(")");
		return script.toString();
	}
	
//	@Override
	public JavaScript getWaitScript(){
		return new JavaScript(getScript());
	}

//	@Override
	public void setLoadText(String loadText) {
		setText(loadText);
		
	}

//	@Override
	public String getLoadText() {
		return getText();
	}

}
